#include "ODN_ModelEntity.h"

#include "ODN_GameMgr.h"

//TODO: Replace this with Ogre resource loading
#define MODEL_PATH "models\\"

namespace ouden {

void CBreakEvent::onActivate()
{
	//Raise the break event of the given entity
	m_pEntity->onBreak();
}

void C_StartMoveEvent::onActivate()
{
	//Raise the start moving event of the given entity
	m_pEntity->onStartMove(m_vDirection);
}

void C_EndMoveEvent::onActivate()
{
	//Raise the start moving event of the given entity
	m_pEntity->onEndMove(m_vDirection);
}



CModelEntity::CModelEntity(String sName) : CBaseEntity(sName)
{
	//ctor
	
}

CModelEntity::~CModelEntity()
{
	//dtor
	
	while (m_pBits.size()) {
		
		delete m_pBits.back();
		m_pBits.pop_back();
		
	}
	
	
}

void CModelEntity::setModel(String sModelName)
{
	
	
	//Load blueprint
	//Load mesh
	//Load model entity description file
	//for each bit in model entity
	//  create model bit with new submesh and solid
	
	//TODO: Finish this function
	
	std::cout << "start of entity constructor" << std::endl;
	
	std::cout << "CGameMgr::getInstance() = " << CGameMgr::getInstance() << std::endl;
	std::cout << "CGameMgr::getInstance()->getSceneMgr() = " << CGameMgr::getInstance()->getSceneMgr() << std::endl;
	/*
	//Load the mesh into an Ogre entity
	Ogre::Entity* pOgreEntity = CGameMgr::getInstance()->getSceneMgr()->createEntity(sName, sModelName + ".mesh");
	pOgreEntity->setMaterialName("Plastic/Red");
	pOgreEntity->setNormaliseNormals(true); // Keep the normals normalized even after scaling.
	return; //TODO: Remove this
	//Load physics blueprint from file
	opal::Blueprint bpBlueprint;
	opal::loadFile(bpBlueprint, MODEL_PATH + sModelName + " blueprint.xml");
	opal::Matrix44r m44Transform;
	m44Transform.translate(0, 0, 0);
	
	//Instantiate the Blueprint.
	opal::BlueprintInstance bpiBlueprintInstance;
	//TODO: Look at scale factor or whatever 16 is for
	CGameMgr::getInstance()->getSimulator()->instantiateBlueprint(bpiBlueprintInstance, bpBlueprint, m44Transform, 16);
	
	if (pOgreEntity->hasSkeleton()) {
		
		//Get a pointer to the ogre skeleton
		Ogre::Skeleton* pSkeleton = pOgreEntity->getSkeleton();
		
		//Loop through the solids in the blueprint
		for (int i=0; i<bpiBlueprintInstance.getNumSolids(); ++i)
		{
			
			//Get the current solid
			opal::Solid* pSolid = bpiBlueprintInstance.getSolid(i);
			
			//Get the current bone
			Ogre::Bone* pBone = pSkeleton->getBone(pSolid->getName());
			
			//Create a new model bit
			CModelBit* pNewModelBit = new CModelBit(pSolid, pBone);
			m_pBits.push_back(pNewModelBit);
			
		}
		
	}
	else
	{
		
		//Get the only solid
		opal::Solid* pSolid = bpiBlueprintInstance.getSolid(0);
		
		//Create a new scenenode
		Ogre::SceneNode* pSceneNode = CGameMgr::getInstance()->getSceneMgr()->getRootSceneNode()->
										createChildSceneNode(pSolid->getName());
		
		//Create a new model bit
		CModelBit* pNewModelBit = new CModelBit(pSolid, pSceneNode);
		m_pBits.push_back(pNewModelBit);
		
	}
	*/
}

} //namespace ouden
